package com.example.weiji_1;

import java.io.File;  

import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  

import android.content.Context;  
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;  
import android.util.Log;  
  

/** 
* 杩欎釜绫诲氨鏄疄鐜颁粠assets鐩綍璇诲彇鏁版嵁搴撴枃浠剁劧鍚庡啓鍏Dcard涓�濡傛灉鍦⊿Dcard涓瓨鍦紝灏辨墦寮�暟鎹簱锛屼笉瀛樺湪灏变粠assets鐩綍涓嬪鍒惰繃鍘�
* @author Big_Adamapple 
* 
*/ 
public class SQLdm{    
        
	//鏁版嵁搴撳瓨鍌ㄨ矾寰�   
    //String filePath = "data/data/com.example.test1/test11.db";  
	String filePath = "data/data/com.example.weiji_1/databases/weiji.db"; 
	//鏁版嵁搴撳瓨鏀剧殑鏂囦欢澶�data/data/com.main.jh 涓嬮潰   
    String pathStr = "data/data/com.example.weiji_1/databases";  
        
    SQLiteDatabase database;     
    public  SQLiteDatabase openDatabase(Context context){    
        //System.out.println("filePath:"+filePath);    
        File jhPath = new File(filePath);    
        //鏌ョ湅鏁版嵁搴撴枃浠舵槸鍚﹀瓨鍦� 
            if(jhPath.exists()){    
                Log.i("test", "瀛樺湪鏁版嵁搴");  
                //瀛樺湪鍒欑洿鎺ヨ繑鍥炴墦寮�殑鏁版嵁搴�  
                return SQLiteDatabase.openOrCreateDatabase(jhPath, null);    
            }else{    
            	//涓嶅瓨鍦ㄥ厛鍒涘缓鏂囦欢澶�
                File path=new File(pathStr);    
                Log.i("test", "pathStr="+path);  
                if (path.mkdir()){    
                    Log.i("test", "鍒涘缓鎴愬姛");   
                }else{    
                    Log.i("test", "鍒涘缓澶辫触");  
                };    
                try {    
                	//寰楀埌璧勬簮    
                	Log.i("test","杩涘叆try");
                	AssetManager am= context.getAssets();
                	Log.i("test","am is ok");
                	//寰楀埌鏁版嵁搴撶殑杈撳叆娴� 
                    InputStream is=am.open("weiji");
                    Log.i("test","inputstream success");
                    Log.i("test", is+"");  
                  //鐢ㄨ緭鍑烘祦鍐欏埌SDcard涓婇潰   
                    FileOutputStream fos=new FileOutputStream(jhPath); 
                    Log.i("test","output success");
                    Log.i("test", "fos="+fos);  
                    Log.i("test", "jhPath="+jhPath);  
                  //鍒涘缓byte鏁扮粍  鐢ㄤ簬1KB鍐欎竴娆�
                    byte[] buffer=new byte[1024];    
                    int count;    
                    while((count = is.read(buffer))>0){    
                        Log.i("test", "寰楀埌");  
                        fos.write(buffer,0,count);    
                    }    
                  //鏈�悗鍏抽棴灏卞彲浠ヤ簡    
                    fos.flush();    
                    fos.close();    
                    is.close();    
                } catch (IOException e) {    
                    // TODO Auto-generated catch block    
                    e.printStackTrace();    
                    return null;  
                }    
                //濡傛灉娌℃湁杩欎釜鏁版嵁搴� 鎴戜滑宸茬粡鎶婁粬鍐欏埌SD鍗′笂浜嗭紝鐒跺悗鍦ㄦ墽琛屼竴娆¤繖涓柟娉�灏卞彲浠ヨ繑鍥炴暟鎹簱浜� 
                return openDatabase(context);    
            }    
    }
}  